package stuff;/*
 * Copyright (c) Jan Šlahora (${USER}) - 2011.
 * Created in IntelliJ IDEA 10.5
 */

import javax.swing.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

/*
*   File stuff.FileManager
*   in package PACKAGE_NAME
*   was created by slaha
*   on 17.11.11 at 12:50.
*
*/
public class FileManager {
  private static Charset kodovani = Charset.forName("UTF-8");

  /**
   * Čte soubor, vrací mapu, kterou přečte ze souboru. (Tvar řádku 'klíč:hodnota'.)
   * @param name jméno souboru ke čtení
   * @return mapa hodnot vyčtených ze souboru
   */
  static public Map<String, String> readFile(String name) {
    Map<String, String> map = null;
    BufferedReader reader = null;
    try {
      reader = Files.newBufferedReader(getFile(name), kodovani);
    } catch (IOException e) {
    }

    if (reader != null) {
      String line = null;
      try {
        map = new HashMap<String, String>();
        while ((line = reader.readLine()) != null) {
          String[] split = line.split(":");
          map.put(split[0], split[1]);
        }
        reader.close();
      } catch (Exception e) {

      }
    }

    return map;
  }

  static public void writeFile(String name, Map<String, String> hodnoty) {
    if (hodnoty == null || name == null) {
      return;
    }
    BufferedWriter writer = null;
    BufferedReader reader = null;
    Path tempPath = makeTemp();
    try {
      reader = Files.newBufferedReader(getFile(name), kodovani);
    } catch (IOException e) {

    }

    if (reader != null) {
      String line = null;
      try {
        while ((line = reader.readLine()) != null) {
          //to, co na tom řádku přečřtu
          String[] split = line.split(":");
          //.. když není v mapě, přidám
          if ((!(hodnoty.containsKey(split[0]))))  {
            hodnoty.put(split[0], split[1]);
          }
        }
      } catch (Exception e) {

      } finally {
        try {
          reader.close();
        } catch (IOException e) {

        }
      }
  }

    try {
      Path file = getFile(name);
      File x = new File(file.toString());
      
      if (!(x.delete())) {
        System.out.println("ddd");
      }
      writer = Files.newBufferedWriter(getFile(name), kodovani);

      for (String s : hodnoty.keySet()) {
        writer.write(s.concat(":").concat(hodnoty.get(s)));
        writer.newLine();
        writer.flush();
      }
      writer.flush();
      writer.close();
    } catch (IOException e) {
      e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
  }

  static private Path getFile(String name) {
    String settDir = getFolder();

    Path file = Paths.get(settDir.concat(System.getProperty("file.separator")).concat(name));

    if (!Files.exists(file)) {
      try {
        Files.createFile(file);
      } catch (IOException e) {
        System.err.println("Can't create stuff.FileManager file");
        JOptionPane.showMessageDialog(null, "Can't create stuff.FileManager file", "Error", JOptionPane.ERROR_MESSAGE);
        e.printStackTrace();
      }
    }
    return file;
  }

 public static String getFolder() {
    Map<String, String> enviroment = System.getenv();
    String settFolder = (enviroment.get("APPDATA") == null)?  enviroment.get("HOME"): enviroment.get("APPDATA");
    settFolder += System.getProperty("file.separator") + ".SVLA";
    if (!Files.exists(Paths.get(settFolder))) {
      try {
        Files.createDirectory(Paths.get(settFolder));
      } catch (IOException e) {
        System.err.println("Can't create stuff.FileManager dir");
        JOptionPane.showMessageDialog(null, "Can't create stuff.FileManager dir", "Error", JOptionPane.ERROR_MESSAGE);
        e.printStackTrace();
      }
    }
    return settFolder;
  }

  private static Path makeTemp() {
    Path tempFile = null;
    try {
      tempFile = Files.createTempFile(null, ".svla");
    } catch (Exception e) {
      
    }
    return tempFile;
  }

  private static void copyTempToFile(String sett, String temp) {
    System.out.println(temp);
  }
}
